{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "import sys\n",
    "\n",
    "from shutil import copyfile\n",
    "import imp\n",
    "\n",
    "#print os.getcwd()\n",
    "\n",
    "def create_run_dir(base_dir=None):\n",
    "    if base_dir is None:\n",
    "        results_dir = os.getcwd() + '/results'\n",
    "    else:\n",
    "        results_dir = base_dir + '/results'\n",
    "    run_num_file = os.path.join(results_dir, \"run_num.txt\")\n",
    "    if not os.path.exists(results_dir):\n",
    "        print \"making results dir\"\n",
    "        os.mkdir(results_dir)\n",
    "\n",
    "    if not os.path.exists(run_num_file):\n",
    "        print \"making run num file....\"\n",
    "        f = open(run_num_file,'w')\n",
    "        f.write('0')\n",
    "        f.close()\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "    f = open(run_num_file,'r+')\n",
    "\n",
    "    run_num = int(f.readline()) + 1\n",
    "\n",
    "    f.seek(0)\n",
    "\n",
    "    f.write(str(run_num))\n",
    "\n",
    "\n",
    "    run_dir = os.path.join(results_dir,'run%i'%(run_num))\n",
    "    os.mkdir(run_dir)\n",
    "    \n",
    "#     if custom_rc:\n",
    "#         make_custom_config_file(run_dir)\n",
    "    return run_dir"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def make_custom_config_file(run_dir):\n",
    "\n",
    "    theano_path = imp.find_module('theano')[1]\n",
    "\n",
    "    copyfile(os.path.join(theano_path,'.theanorc'), os.path.join(run_dir, '.theanorc'))\n",
    "\n",
    "    #make the compile dir be this unique run dir to lock issues\n",
    "    with open(os.path.join(run_dir, '.theanorc'), \"a\") as f:\n",
    "        f.write('[base_compiledir]')\n",
    "        f.write(run_dir)\n",
    "      \n",
    "    #point to new config file\n",
    "    os.environ['THEANORC'] = os.path.join(run_dir, '.theanorc')\n",
    "    f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
